package com.winit.openapi.apiadapter.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.OutboundOrderService;
import com.winit.oms.spi.outbound.command.order.SubmitOutboundOrderCommand;
import com.winit.oms.spi.vo.v2.response.ErrorResult;
import com.winit.oms.spi.vo.v2.response.Response;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiAdapteException;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 提交海外仓出库单适配
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2016年12月29日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
public class ConfirmOrderAdapteController extends AbstractAdapteController {

    @Resource
    private OutboundOrderService outboundOrderService;

    @RequestMapping(value = "/confirmOutboundOrder")
    public String confirmOutboundOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        return doAdapte(requestMsg, responseMsg);
    }

    @Override
    public boolean checkCustomerTansferConfig(RequestMsg requestMsg, ResponseMsg responseMsg) {
        
//        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
//        String orderNo = json.getString("outboundOrderNum");

        return true;
    }

    @Override
    public void doAdapteInOpenAPI(RequestMsg requestMsg, ResponseMsg responseMsg) {
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        String orderNo = json.getString("outboundOrderNum");
        SubmitOutboundOrderCommand command = new SubmitOutboundOrderCommand();
        List<String> orderNoList = new ArrayList<String>();
        orderNoList.add(orderNo);
        command.setOrderNoList(orderNoList);
        Response response = outboundOrderService.submitOrder(command);
        if (response != null) {
            if (response.getIsSuccess() || CollectionUtils.isEmpty(response.getErrorResultList())) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("outboundOrderNum", orderNo);
                responseMsg.setData(map);
            } else {
                if (!CollectionUtils.isEmpty(response.getErrorResultList())) {
                    ErrorResult errorResult = response.getErrorResultList().get(0);
                    throw new ApiAdapteException(errorResult.getErrorCode(), errorResult.getErrorMsg());
                }

            }

        }
    }

    @Override
    protected void validateInputJson(RequestMsg requestMsg) {
        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        if (null == json) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 订单号必填
        ValidateUtil.validNotNull(json, "outboundOrderNum");
        ValidateUtil.validMaxLength(json, "outboundOrderNum", 64, true);

    }

}
